package com.compass.controller;

import com.compass.entity.User;
import com.compass.service.UserService;
import com.compass.vo.LoginUser;
import com.compass.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/3/10
 * Time: 14:02
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/login")
@SessionAttributes(value = {"loginUser"})
@Slf4j
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/login/login");
        return modelAndView;
    }

    @RequestMapping("/registerPage")
    public ModelAndView registerPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/login/register");
        return modelAndView;
    }


    @RequestMapping("/login")
    public ModelAndView login(@Valid User user, BindingResult bindingResult, ModelMap modelMap) {
        ModelAndView modelAndView = new ModelAndView();
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            modelAndView.addObject("msg", msg);
            modelAndView.addObject("user", user);
            modelAndView.setViewName("/login/loginPage");
            return modelAndView;
        }
        ResponseMessage responseObj = userService.checkUser(user);
        if (!responseObj.isSuccess()) {
            modelAndView.addObject("msg", responseObj.getMsg());
            modelAndView.setViewName("/login/loginPage");
            return modelAndView;
        }
        LoginUser loginUser = (LoginUser) responseObj.getObj();
        modelMap.addAttribute("loginUser", loginUser);
        log.info("{}登录成功",loginUser.getUserName());
        modelAndView.setViewName("redirect:/index");
        return modelAndView;
    }

    @RequestMapping("/register")
    public ModelAndView register(@Valid User user, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            modelAndView.addObject("msg", msg);
            modelAndView.addObject("user", user);
            modelAndView.setViewName("/login/registerPage");
            return modelAndView;
        }
        ResponseMessage responseObj = userService.register(user);
        if (!responseObj.isSuccess()) {
            modelAndView.addObject("msg", responseObj.getMsg());
            modelAndView.setViewName("/login/registerPage");
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/login/loginPage");
        return modelAndView;
    }
    @RequestMapping("/loginout")
    public String loginout(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "redirect:/login/loginPage";
    }
}
